//
//  WSRecommendDetailController.m
//  GroupFourFM
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSRecommendDetailController.h"
#import "WSPlayViewController.h"
#import "WSResource.h"
#import "WSTrackModel.h"
#import "WSAlbumModel.h"
#import "WSHeadView.h"
#import "WSInfoModel.h"
#import "WSAppTools.h"
#import "WSAlbumDetailTableViewCell.h"
#import "WSListHeadView.h"
#import "WSAlbumsDetailViewController.h"
#import "WHAuthorDetailViewController.h"

@interface WSRecommendDetailController ()<UITableViewDataSource, UITableViewDelegate, WSPlayViewControllerDelegate>

@property (nonatomic, strong) UITableView *musicListTableView;
@property (nonatomic, strong) NSMutableArray *musicListArray;
@property (nonatomic, assign) CGFloat cellHeight;
@property (nonatomic, copy) NSString *cellCategory;
@property (nonatomic, strong) WSHeadView *headView;

@end

@implementation WSRecommendDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //初始化数组
    self.musicListArray = [NSMutableArray array];
    
    [self getData];
    [self createTableView];
    self.navigationItem.title = @"听单";
    
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0 , 20, 30);
    backButton.backgroundColor = CLEAR;
    [backButton setImage:[UIImage imageNamed:@"btn_back_n@2x.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"btn_back_h@2x.png"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

#pragma mark - 返回按钮方法
-(void)backButtonAction:(UIButton *)btn {
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)buttonAction:(UIButton *)btn
{
    
    //    WSPlayViewController *player = [WSPlayViewController sharaMusicPlayer];
    //    WSNavigationBarController *naVC = [[WSNavigationBarController alloc] initWithRootViewController:player];
    //    TOTrackModel *sing = [self.allTrackArray objectAtIndex:indexPath.row];
    //    player.song_id = sing.trackId;
    //    player.allMusicArray = self.allTrackArray;
    //    player.indexRow = indexPath.row;
    //    [self.view.window.rootViewController presentViewController:naVC animated:YES completion:^{
    //
    //    }];
    
}

#pragma mark - 创建tableView
-(void)createTableView {
    
    self.musicListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT_NOBAR) style:UITableViewStylePlain];
    self.musicListTableView.backgroundColor = COLOR_BG;
    [self.view addSubview:self.musicListTableView];
    self.musicListTableView.dataSource = self;
    self.musicListTableView.delegate = self;
    self.musicListTableView.sectionHeaderHeight = 40;
    self.musicListTableView.rowHeight = 120;
    [self.musicListTableView registerClass:[WSAlbumDetailTableViewCell class] forCellReuseIdentifier:@"musicListTableCell"];
    self.musicListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.musicListTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    self.headView = [[WSHeadView alloc] init];
}

#pragma mark - tableView协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.musicListArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    if ([self.cellCategory isEqualToString:@"2"]) {
        WSAlbumDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"musicListTableCell"];
        WSTrackModel *model = [self.musicListArray objectAtIndex:indexPath.row];
        cell.trackModel = model;
//        cell.playCount.text = [NSString stringWithFormat:@"%@",model.playsCounts];
//        
//        self.cellHeight = cell.playCount.frame.origin.y + cell.playCount.frame.size.height + 10;
        return cell;
//    }
//    else
//    {
//        TOAlbumListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"albumListTableCell"];
//        TOAlbumModel *model = [self.musicListArray objectAtIndex:indexPath.row];
//        cell.albumModel = model;
//        self.cellHeight = cell.newsPic.frame.origin.y + cell.newsPic.frame.size.height + 10;
//        return cell;
//    }
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    WSListHeadView *headView = [[WSListHeadView alloc] init];
    headView.title = @"听单推荐";
    return headView;
}

#pragma mark tableView点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if ([self.cellCategory isEqualToString:@"2"]) {
        WSPlayViewController *player = [WSPlayViewController sharaMusicPlayer];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
        WSTrackModel *sing = [self.musicListArray objectAtIndex:indexPath.row];
        player.delegate = self;
        player.song_id = sing.trackId;
        player.allMusicArray = self.musicListArray;
        player.indexRow = indexPath.row;
        [self presentViewController:naVC animated:YES completion:^{
            
            NSLog(@"hello");
        }];
        
    }
    else
    {
        WSAlbumsDetailViewController *albumsDetailVC = [[WSAlbumsDetailViewController alloc] init];
        
        albumsDetailVC.albumId = ((WSAlbumModel *)[self.musicListArray objectAtIndex:indexPath.row]).myId;
        albumsDetailVC.myTitle = _myTitle;
        [self.navigationController pushViewController:albumsDetailVC animated:YES];
    }
    
}

#pragma mark - 请求数据

-(void)getData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_detail?device=iphone&id=%@&position=1&title=精品听单", self.specialId];
    [WSResource getDataWithURL:urlStr block:^(id result) {
        
        NSMutableDictionary *infoDic = [result objectForKey:@"info"];
        WSInfoModel *infoModel = [WSInfoModel modelWithDictionary:infoDic];
        infoModel.height = [WSAppTools heightForContent:infoModel.intro width:SCREEN_WIDTH * 0.8 size:15];
        self.headView.infoModel = infoModel;
        self.cellCategory = [NSString stringWithFormat:@"%@", [infoDic objectForKey:@"contentType"]];
        
        self.headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, infoModel.height + 80);
        self.musicListTableView.tableHeaderView = self.headView;
        
        NSMutableArray *dataArray = [result objectForKey:@"list"];
        
        for (NSMutableDictionary *dic in dataArray) {
            
            if ([self.cellCategory isEqualToString:@"2"]) {
                WSTrackModel *model = [WSTrackModel modelWithDictionary:dic];
                [self.musicListArray addObject:model];
            }
            else{
                WSAlbumModel *model = [WSAlbumModel modelWithDictionary:dic];
                model.isFavor = NO;
                
                if ([[[WSFMData shareFMDB] selectAlbumIdData] containsObject:model.myId]) {
                    model.isFavor = YES;
                }
                
                [self.musicListArray addObject:model];
            }
        }
        
        [self.musicListTableView reloadData];
    } error:^(NSError *error) {
        
        NSLog(@"请求失败  error");
    }];
}

#pragma mark WSPlayViewControllerDelegate
- (void)changePageData:(NSInteger)id title:(NSString *)title{
    
    WSAlbumsDetailViewController *albumsDetailVC = [[WSAlbumsDetailViewController alloc] init];
    
    [(WSTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
    albumsDetailVC.albumId = [NSString stringWithFormat:@"%ld", id];
    albumsDetailVC.myTitle = title;
    [self.navigationController pushViewController:albumsDetailVC animated:YES];
}

- (void)pushNextViewC:(NSInteger)uid title:(NSString *)title {
    
    WHAuthorDetailViewController *authorDVC = [[WHAuthorDetailViewController alloc] init];
    authorDVC.uid = uid;
    authorDVC.title = title;
    [self.navigationController pushViewController:authorDVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
